package com.fruits.service.impl;

import com.fruits.dao.CartDao;
import com.fruits.dao.FruitDao;
import com.fruits.dao.impl.CartDaoImpl;
import com.fruits.dao.impl.FruitDaoImpl;
import com.fruits.domain.Cart;
import com.fruits.domain.Fruit;
import com.fruits.service.CartService;

import java.util.ArrayList;
import java.util.List;

public class CartServiceImpl implements CartService {
   private CartDao cartDao=new CartDaoImpl();
   private FruitDao fruitDao=new FruitDaoImpl();
    @Override
    public boolean add(int uid, Cart cart) {
        int add=cartDao.add(uid,cart);
        if(add==1){
            return true;
        }
        return false;
    }
    @Override
    public Cart find(int uid, int fid) {
        return cartDao.find(uid,fid);
    }

    @Override
    public boolean update(int uid, Cart cart) {
        int update=cartDao.update(uid,cart);
        if(update==1){
            return true;
        }
        return false;
    }

    @Override
    public List<Cart> findById(int uid) {
        return cartDao.findById(uid);
    }

    @Override
    public List<Fruit> show(int uid, boolean flag) {
    List<Cart> carts=findById(uid);
     List<Fruit> fruits=new ArrayList<>();

     if(flag){//购物车
          for(Cart cart:carts){
              if(cart.isCart()){
                  Fruit fruit=fruitDao.findByFid(cart.getFid());
                    fruits.add(fruit);
              }
          }
      }else{
         for(Cart cart:carts){
              if(cart.isStar()){
                  Fruit fruit=fruitDao.findByFid(cart.getFid());
                    fruits.add(fruit);
              }
          }
     }
     return fruits;
    }

    @Override
    public boolean delete(int uid, int fid) {
        int delete = cartDao.delete(uid, fid);
        if(delete==1){
            return true;
        }
        return false;
    }
}
